package com.polaris.live.ui.luckybag.fragment.dialog

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.viewpager2.widget.ViewPager2
import com.polaris.live.common.adapter.ViewPagerAdapter
import com.polaris.live.common.ext.dp
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.util.NavigationBarUtils
import com.polaris.live.databinding.FragmentDialogLuckyBagHistoryBinding
import com.polaris.live.ui.luckybag.fragment.LuckyBagHistoryFragment
import com.polaris.live.ui.message.fragment.dialog.BottomDialogFragment

/**
 * 福袋历史弹窗
 *
 * @author Created by lucas on 2024/6/11 06:42
 * @since 1.0.0
 */
class LuckyBagHistoryDialogFragment : BottomDialogFragment() {

    private lateinit var mBinding: FragmentDialogLuckyBagHistoryBinding

    override fun getTopMargin(): Int {
        return 148.dp
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        return FragmentDialogLuckyBagHistoryBinding.inflate(inflater, container, false).let {
            mBinding = it

            it.root
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        initView()
    }

    private fun initView() {
        val fragments = listOf(
            LuckyBagHistoryFragment().apply {
                arguments = bundleOf("type" to LuckyBagHistoryFragment.RECEIVED)
            },
            LuckyBagHistoryFragment().apply {
                arguments = bundleOf("type" to LuckyBagHistoryFragment.SEND)
            }
        )

        mBinding.apply {
            vpPage.offscreenPageLimit = 2
            vpPage.adapter = ViewPagerAdapter(fragments, this@LuckyBagHistoryDialogFragment)
            vpPage.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    if (position == 0) {
                        changeSelect(LuckyBagHistoryFragment.RECEIVED)
                    } else {
                        changeSelect(LuckyBagHistoryFragment.SEND)
                    }
                }
            })
            tvReceived.setOnSingleClickListener {
                vpPage.currentItem = 0
                changeSelect(LuckyBagHistoryFragment.RECEIVED)
            }
            tvSend.setOnSingleClickListener {
                vpPage.currentItem = 1
                changeSelect(LuckyBagHistoryFragment.SEND)
            }
            bvTitle.setOnLeftClientListener {
                dismiss()
            }
            changeSelect(LuckyBagHistoryFragment.RECEIVED)

            NavigationBarUtils.setNavigationHeightIfTransparent(vBottom)
        }
    }

    private fun changeSelect(type: Int) {
        if (type == LuckyBagHistoryFragment.RECEIVED) {
            mBinding.tvReceived.isSelected = true
            mBinding.tvSend.isSelected = false
        } else {
            mBinding.tvReceived.isSelected = false
            mBinding.tvSend.isSelected = true
        }
    }

    companion object {

        const val FRAGMENT_TAG = "LuckyBagHistoryDialogFragment"
    }
}